/**
 * 作者：致心 vue项目所有文件命名全部采用中划线分隔   typeOfUtils
 * 创建时间：2021/3/5 14:50
 */

const typeOfUtils = {
  isString(value) {
    return Object.prototype.toString.call(value) === '[object String]'
  },
  isNotString(value) {
    return !this.isString(value)
  },
  isNumber(value) {
    return Object.prototype.toString.call(value) === '[object Number]'
  },
  isNotNumber(value) {
    return !this.isNumber(value)
  },
  isFloat(value) {
    if (this.isNotNumber(value)) {
      return false
    }
    const string = value.toString()
    return string.indexOf('.') !== -1
  },
  isNotFloat(value) {
    return !this.isFloat(value)
  },
  isBoolean(value) {
    return Object.prototype.toString.call(value) === '[object Boolean]'
  },
  isNotBoolean(value) {
    return !this.isBoolean(value)
  },
  isSymbol(value) {
    return Object.prototype.toString.call(value) === '[object Symbol]'
  },
  isNotSymbol(value) {
    return !this.isSymbol(value)
  },
  isUndefined(value) {
    return Object.prototype.toString.call(value) === '[object Undefined]'
  },
  isNotUndefined(value) {
    return !this.isUndefined(value)
  },
  isNull(value) {
    return Object.prototype.toString.call(value) === '[object Null]'
  },
  isNotNull(value) {
    return !this.isNull(value)
  },
  isFunction(value) {
    return Object.prototype.toString.call(value) === '[object Function]'
  },
  isNotFunction(value) {
    return !this.isFunction(value)
  },
  isDate(value) {
    return Object.prototype.toString.call(value) === '[object Date]'
  },
  isNotDate(value) {
    return !this.isDate(value)
  },
  isArray(value) {
    return Object.prototype.toString.call(value) === '[object Array]'
  },
  isNotArray(value) {
    return !this.isArray(value)
  },
  isRegExp(value) {
    return Object.prototype.toString.call(value) === '[object RegExp]'
  },
  isNotRegExp(value) {
    return !this.isRegExp(value)
  },
  isError(value) {
    return Object.prototype.toString.call(value) === '[object Error]'
  },
  isNotError(value) {
    return !this.isError(value)
  },
  isHTMLDocument(value) {
    const d = document.createElement('div')
    try {
      d.appendChild(value.cloneNode(true))
      return value.nodeType === 1
    } catch (e) {
      return value === window || value === document
    }
  },
  isNotHTMLDocument(value) {
    return !this.isHTMLDocument(value)
  },
  isGlobal(value) {
    return Object.prototype.toString.call(value) === '[object global]'
  },
  isNotGlobal(value) {
    return !this.isGlobal(value)
  },
  isObject(value) {
    return Object.prototype.toString.call(value) === '[object Object]'
  },
  isNotObject(value) {
    return !this.isObject(value)
  },
  isPromise(value) {
    return Object.prototype.toString.call(value) === '[object Promise]'
  },
  isNotPromise(value) {
    return !this.isPromise(value)
  },
  /**
   * @Description :判断值是不是空，判断为空的数据有 [],{},'',NaN,null,Undefined
   * @Author :致心
   * @Date :2021/3/10 17:01
   * @param value
   * @returns {boolean}
   */
  isEmpty(value) {
    if (this.isNull(value) || this.isUndefined(value)) {
      return true
    }
    if (this.isArray(value) && value.length === 0) {
      return true
    }
    if (this.isString(value) && value.length === 0) {
      return true
    }
    if (this.isNumber(value) && isNaN(value)) {
      return true
    }
    if (this.isObject(value)) {
      if (Object.keys(value).length === 0) {
        return true
      } else {
        const list = Object.keys(value)
        let count = 0
        list.forEach(i => {
          if (this.isEmpty(value[i])) {
            count++
          }
        })
        return count === list.length
      }
    }
    return false
  },
  /**
   * @Description :判断值是不是不为空
   * @Author :致心
   * @Date :2021/3/31 10:19
   * @param value
   * @returns {boolean}
   */
  isNotEmpty(value) {
    return !this.isEmpty(value)
  },
  /**
   * @Description :判断对象里面多个属性不为空
   * @Author :致心
   * @Date :2021/4/29 15:41
   * @param value {Object}
   * @param attrs {Array} 传入字符数组
   * @returns {boolean}
   */
  objectAttrsNotEmpty(value, attrs) {
    if (this.isNotObject(value)) {
      throw new Error('传入的value不是对象')
    }
    if (this.isNotArray(attrs)) {
      throw new Error('传入的attrs不是数组')
    }
    if (this.isEmpty(value)) {
      throw new Error('传入的value不能为空')
    }
    if (this.isEmpty(attrs)) {
      throw new Error('传入的attrs不能为空')
    }
    let count = 0
    attrs.forEach(i => {
      if (this.isString(i)) {
        count++
      }
    })
    if (count !== attrs.length) {
      throw new Error('传入的attrs有数据错误')
    }
    count = 0
    attrs.forEach(i => {
      if (this.isNotEmpty(value[i])) {
        count++
      }
    })
    return count === attrs.length
  },
  /**
   * @Description :判断对象里面多个属性有空
   * @Author :致心
   * @Date :2021/4/30 15:03
   * @param value {Object}
   * @param attrs {Array} 传入字符数组
   * @returns {boolean}
   */
  objectAttrsHasEmpty(value, attrs) {
    return !this.objectAttrsNotEmpty(value, attrs)
  }

}

export default typeOfUtils
